package com.softline.documentsmall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softline.documentsmall.entity.*;
import com.softline.documentsmall.ret.ReturnVO;
import com.softline.documentsmall.ret.ReturnVOUtils;
import com.softline.documentsmall.service.ThumbsUpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: documentsmall
 * @description: 点赞controller
 * @author: wmj
 * @create: 2020-12-09
 * @ClassName: ThumbsUpController
 */

@Api(value="点赞相关接口",tags={"点赞相关接口"})
@RestController
@RequestMapping("/thumbs/up")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class ThumbsUpController {

    @Autowired
    ThumbsUpService thumbsUpService;

    @ApiOperation(value="个人中心点赞列表页", notes = "个人中心点赞列表页")
    @GetMapping(value = "/listByPersonal")
    public ReturnVO listByPersonal(@RequestParam(name = "passportId")String passportId,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,@RequestParam(name = "type",defaultValue = "en")String type){
        QueryWrapper<ThumbsUp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("passport_id",passportId);
        Page<ThumbsUpVO> result = new Page<>();
        List<ThumbsUpVO> records = thumbsUpService.getReconds(passportId,type,pageNo,pageSize);
        Long count = Long.valueOf(thumbsUpService.count(queryWrapper));
        result.setRecords(records);
        result.setCurrent(pageNo);
        result.setSize(pageSize);
        result.setTotal(count);
        return ReturnVOUtils.success(result);
    }

    @ApiOperation(value="新增", notes = "新增")
    @PostMapping(value = "/add")
    public ReturnVO add(@RequestBody ThumbsUp thumbsUp){
        int add = thumbsUpService.add(thumbsUp);
        if(add == 1){
            return ReturnVOUtils.success("点赞成功");
        }else {
            return ReturnVOUtils.businessFailCustom(201,"您已点赞过该词条");
        }
    }

    @ApiOperation(value="根据词条id和用户id删除点赞记录", notes = "根据词条id和用户id删除点赞记录")
    @GetMapping(value = "/delete")
    public ReturnVO delete(@RequestParam("passportId") String passportId,@RequestParam("dictionaryId") Integer dictionaryId){
        thumbsUpService.delete(passportId,dictionaryId);
        return ReturnVOUtils.success("取消点赞成功");
    }

    @ApiOperation(value="根据id删除点赞记录", notes = "根据id删除点赞记录")
    @DeleteMapping(value = "/deleteById")
    public ReturnVO deleteById(@RequestParam("id") Integer id){
        thumbsUpService.removeById(id);
        return ReturnVOUtils.success("取消点赞成功");
    }

    @ApiOperation(value="根据词条id和用户查询是否点赞，1：收藏，0：未收藏", notes = "根据词条id和用户查询是否点赞，1：收藏，0：未收藏")
    @GetMapping(value = "/isThumbsUp")
    public ReturnVO isThumbsUp(@RequestParam("passportId") String passportId,@RequestParam("dictionaryId") Integer dictionaryId){
        ThumbsUp thumbsUp = thumbsUpService.isThumbsUp(passportId,dictionaryId);
        QueryWrapper<ThumbsUp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dictionary_id",dictionaryId);
        int count = thumbsUpService.count(queryWrapper);
        Map<String,Object> result = new HashMap<>();
        if (thumbsUp == null){
            result.put("isThumbsUp","0");
            result.put("count",count);
            return ReturnVOUtils.success(result);
        }else {
            result.put("isThumbsUp","1");
            result.put("count",count);
            return ReturnVOUtils.success(result);
        }
    }
}
